/*
    Copyright (c) 2006-2007 Ivaylo Ivanov

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/
package com.avalonsql.core.configs;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.IOException;

public class ConnectionConfig extends Config {

    protected BASE64Encoder encoder = new BASE64Encoder();
    protected BASE64Decoder decoder = new BASE64Decoder();

    public ConnectionConfig(String name, String propertiesFile, String driverDefinition,
                        String connectionURL, String userName, String password, String defaultCatalog) {
        super(propertiesFile);
        setName(name);
        setDriverDefinition(driverDefinition);
        setConnectionUrl(connectionURL);
        setUserName(userName);
        setPassword(password);
        setDefaultCatalog(defaultCatalog);
    }

    public ConnectionConfig(String propertiesFile) {
        super(propertiesFile);
    }

    public void delete() {
        new File(propertiesFile).delete();
    }

    public ConnectionConfig reload() throws ConfigIOException {
        ConnectionConfig result = (ConnectionConfig) super.reload();
        try {
            setPassword(
            new String(decoder.decodeBuffer(getPassword()))
            );
        } catch (IOException e) {
            throw new ConfigIOException(e);
        }
        return result;
    }

    public void save() throws IOException {
        String password = getPassword();
        try {
            setPassword(
                    encoder.encode(password.getBytes("UTF8"))
            );
            super.save();
        } finally {
            setPassword(password);
        }
    }

    public String getName() {
        return getProperty("name");
    }

    public void setName(String name) {
        setProperty("name", name);
    }

    public String getDriverDefinition() {
        return getProperty("driverDefinition");
    }

    public void setDriverDefinition(String driverDefinition) {
        setProperty("driverDefinition", driverDefinition);
    }

    public String getConnectionURL() {
        return getProperty("connectionURL");
    }

    public void setConnectionUrl(String connectionUrl) {
        setProperty("connectionURL", connectionUrl);
    }

    public String getPassword() {
        return getProperty("password");
    }

    public void setPassword(String password) {
        setProperty("password", password);
    }

    public String getUserName() {
        return getProperty("userName");
    }

    public void setUserName(String userName) {
        setProperty("userName", userName);
    }

    public String getDefaultCatalog() {
        return getProperty("defaultCatalog");
    }

    public void setDefaultCatalog(String catalog) {
        setProperty("defaultCatalog", catalog);
    }

}
